define([ "esri/geometry/Point", "esri/symbols/SimpleFillSymbol", "esri/tasks/query", "esri/tasks/IdentifyTask", "util/config/constant", "esri/tasks/IdentifyParameters", "util/map/map", "util/api/project", 
	"util/config/config", "util/map/toolbar/graphic", "util/wechat/wechat", "esri/graphic", "esri/layers/GraphicsLayer",
	"esri/SpatialReference", "esri/symbols/SimpleMarkerSymbol" ], function(Point, SimpleFillSymbol, Query, IdentifyTask, constantUtil,
	IdentifyParameters, mapUtil, projectUtil,  configUtil, graphicUtil, wechatUtil, Graphic, GraphicsLayer, SpatialReference, SimpleMarkerSymbol) {
	return {
		global_variable_fid : null,
		searchInfoOnMapByPoint : function(point, serviceUrl, layerIds) {
			var map = new mapUtil();
			var identifyTask = new IdentifyTask(serviceUrl);
			var identifyParams = new IdentifyParameters();
			identifyParams.returnGeometry = true;
			identifyParams.tolerance = 4;
			identifyParams.layerIds = layerIds;
			identifyParams.layerOption = IdentifyParameters.LAYER_OPTION_ALL;
			identifyParams.width = map.width;
			identifyParams.height = map.height;
			identifyParams.spatialReference = map.spatialReference;
			identifyParams.mapExtent = map.extent;
			identifyParams.geometry = point;
			identifyTask.execute(identifyParams, function(response) {
				if (typeof (response[0]) != "undefined") {
					var fea = response[0].feature;
					return fea;
				} else {
					return null;
				}
			});
		},
		searchInfoOnMapByByPolygon : function(polygon, serviceUrl, layerIds) {
			var map = new mapUtil();
			var identifyTask = new IdentifyTask(serviceUrl);
			var identifyParams = new IdentifyParameters();
			identifyParams.returnGeometry = true;
			identifyParams.tolerance = 4;
			identifyParams.layerIds = layerIds;
			identifyParams.layerOption = IdentifyParameters.LAYER_OPTION_ALL;
			identifyParams.mapExtent = map.__proto__.baselayer.fullExtent;
			identifyParams.geometry = geo;
			var that = this;
			identifyTask.execute(identifyParams, function(response) {
				return response;
			});
		},
		getMapInfoByFid : function(fid) {
			var queryTask = new esri.tasks.QueryTask(constantUtil.LYXB_URL + "/0");
			var query = new Query();
			query.outFields = [ 'FID', '县', '乡', '村', '社', '小班号', '小地名', '面积' ];
			query.where = "FID = " + fid;
			query.returnGeometry = false;
			queryTask.execute(query, function(featureSet) {
				var mapInfos = featureSet.features;
				for (var i = 0; i < mapInfos.length; i++) {
					var fid = mapInfos[i].attributes['FID'];
					var qx = mapInfos[i].attributes['县'];
					var xm = mapInfos[i].attributes['乡'];
					var cm = mapInfos[i].attributes['村'];
					var sm = mapInfos[i].attributes['社'];
					var xbh = mapInfos[i].attributes['小班号'];
					var xdm = mapInfos[i].attributes['小地名'];
					var mj = mapInfos[i].attributes['面积'];
					var population = "fid=" + fid + "&xzm=" + xm + "&cm=" + cm + "&sm=" + sm + "&xbh=" + xbh + "&xdm=" + xdm + "&mj=" + mj
					var project = new projectUtil();
					project.addPopulation(population);
					$("#hid_id").val(fid);
					$("#inv_xzlc").html(xm);
					$("#inv_xbh").html(xbh);
					$("#inv_xbmc").html(xm + cm + sm + "社" + xbh);
					if (typeof ($('#xb_mj')) != "undefined") {
						$("#xb_mj").html(mj);
					}
					if (typeof ($('#xb_xm')) != "undefined") {
						$("#xb_xm").html(qx);
					}
					if (typeof ($('#xb_xzm')) != "undefined") {
						$("#xb_xzm").html(xm);
					}
					if (typeof ($('#xb_cm')) != "undefined") {
						$("#xb_cm").html(cm);
					}
					if (typeof ($('#xb_sm')) != "undefined") {
						$("#xb_sm").html(sm + "社");
					}
					if (typeof ($('#xb_xdm')) != "undefined") {
						$("#xb_xdm").html(xdm);
					}
					if (typeof ($('#xb_xbh')) != "undefined") {
						$("#xb_xbh").html(xbh);
					}
				}
			});
		},
		clickMapXbInfo : function(map, geo) {
			var identifyTask = new IdentifyTask(constantUtil.LYXB_URL);
			var identifyParams = new IdentifyParameters();
			identifyParams.returnGeometry = true;
			identifyParams.tolerance = 10;
			identifyParams.layerIds = [ 0 ];
			identifyParams.layerOption = IdentifyParameters.LAYER_OPTION_ALL;
			identifyParams.width = map.width;
			identifyParams.height = map.height;
			identifyParams.spatialReference = map.spatialReference;
			identifyParams.mapExtent = map.extent;
			identifyParams.geometry = geo;
			var that = this;
			identifyTask.execute(identifyParams, function(response) {
				if (typeof (response[0]) != "undefined") {
					var fea = response[0].feature;
					var fid = fea.attributes['FID'];
					var f = [];
					f.push(fea);
					if (fid != $('#fid').html()) {
						map.graphics.clear();
						that.getLyxbLayerInfo(fea);
						var project = new projectUtil();
						project.getPopulationByFid(fid);
						var mys = [ 255, 0, 255, 0.3 ];
						var xys = [ 255, 0, 255 ];
						graphicUtil.addGraToMap(map, f, mys, xys, map.graphics);
					}
					$.openPanel("#panel-lyxb-info");
				}
			});
		},
		getGlandInfo : function(geo) {
			var pageName = $('#pageName').val();
			$("#dom_picker").html("<input type='text' id='picker' />");
			var map = new mapUtil();
			var identifyTask = new IdentifyTask(constantUtil.LYXB_URL);
			var identifyParams = new IdentifyParameters();
			identifyParams.returnGeometry = false;
			identifyParams.tolerance = 4;
			identifyParams.layerIds = [ 0 ];
			identifyParams.mapExtent = map.__proto__.baselayer.fullExtent;
			identifyParams.geometry = geo;
			var that = this;
			identifyTask.execute(identifyParams, function(response) {
				if(response.length == 0){
					$.alert("此处无小班信息，无法录入信息！");
				}else if (response.length == 1) {
					var fea = response[0].feature;
					var fid = fea.attributes['FID'];
					var project = new projectUtil();
					if (project.checkCheckAccept(fid) == "true") {
						var qx = fea.attributes['县'];
						var xm = fea.attributes['乡'];
						var cm = fea.attributes['村'];
						var sm = fea.attributes['社'];
						var xbh = fea.attributes['小班号'];
						var xdm = fea.attributes['小地名'];
						var mj = fea.attributes['面积'];
						var population = "fid=" + fid + "&xzm=" + xm + "&cm=" + cm + "&sm=" + sm + "&xbh=" + xbh + "&xdm=" + xdm + "&mj=" + mj
						project.addPopulation(population);
						that.getLyxbLayerInfo(fea);
						$('#hid_id').val(fid)
						$('#hid_input_fid').val(fid);
						$.popup(".popup-check-accept");
					} else {
						$.alert(project.checkCheckAccept(fid));
					}
				} else {
					var values = new Array();
					for (var i = 0; i < response.length; i++) {
						var fea = response[i].feature;
						var fid = fea.attributes['FID'];
						var xzm = fea.attributes['乡'];
						var cm = fea.attributes['村'];
						var sm = fea.attributes['社'];
						var xbh = fea.attributes['小班号'];
						var mc = xzm + cm + sm + "社" + xbh + "_" + fid;
						values.push(mc);
					}
					$("#picker").picker({
						toolbarTemplate : '<header class="bar bar-nav">\
						  <button class="button button-link pull-left" id="locationToward" >定位</button>\
						  <button class="button button-link pull-right close-picker" id="checkToward_' + pageName + '">确定</button>\
						  <h1 class="title">请选择小班</h1>\
						  </header>',
						cols : [
							{
								textAlign : 'center',
								values : values
							}
						]
					});
					$("#picker").picker("open");
				}
			});
		},
		getPopulationInfoAndLocationByFid : function(fid, map) {
			if (this.global_variable_fid != fid) {
				map.graphics.clear();
				var locationGraph = new GraphicsLayer();
				projectUtil.listYczs(fid, map);
				this.global_variable_fid = fid;
				$('#li_reason').hide();
				$('#select_qualified').val("1");
				var queryTask = new esri.tasks.QueryTask(constantUtil.LYXB_URL + "/0");
				var query = new Query();
				query.outFields = [ "*" ];
				query.where = "fid = " + fid + "";
				query.returnGeometry = true;
				queryTask.execute(query, function(featureSet) {
					var response = featureSet.features;
					if (typeof (response[0]) != "undefined") {
						var fea = response[0];
						if (fid != $('#fid').html()) {
							this.getLyxbLayerInfo(fea);
							var project = new projectUtil();
							project.getPopulationByFid(fid);
							var mys = [ 0, 245, 255, 0.3 ];
							var xys = [ 0, 245, 255 ];
							graphicUtil.addGraToMap(map, response, mys, xys, map.graphics);
						}
					}
				});
			}
		},
		searchLyxbInMapByFid : function(fid, map) {
			if (this.global_variable_fid != fid) {
				map.graphics.clear();
				var queryTask = new esri.tasks.QueryTask(constantUtil.LYXB_URL + "/0");
				var query = new Query();
				query.outFields = [ "*" ];
				query.where = "fid = " + fid + "";
				query.returnGeometry = true;
				queryTask.execute(query, function(featureSet) {
					var response = featureSet.features;
					if (typeof (response[0]) != "undefined") {
						var fea = response[0];
						if (fid != $('#fid').html()) {
							var mys = [ 0, 245, 255, 0.3 ];
							var xys = [ 0, 245, 255 ];
							graphicUtil.addGraToMap(map, response, mys, xys, map.graphics);
						}
					}
				});
			}
		},
		getLyxbLayerInfo : function(fea) {
			if ($("#fid").length > 0) {
				var fid = fea.attributes['FID'];
				$('#fid').html(fid);
			}
			var area = fea.attributes['面积'];
			$('#area').html(area);
			$('#xb_mj').html(area);
			var xm = fea.attributes['县'];
			$('#xm').html(xm);
			$('#xb_xm').html(xm);
			var xzm = fea.attributes['乡'];
			$('#xzm').html(xzm);
			$('#xb_xzm').html(xzm);
			var cm = fea.attributes['村'];
			$('#cm').html(cm);
			$('#xb_cm').html(cm);
			var sm = fea.attributes['社'];
			$('#sm').html(sm);
			$('#xb_sm').html(sm);
			var xbh = fea.attributes['小班号'];
			if ($("#xbh").length > 0) {
				$('#xbh').html(xbh);
			}
			$('#xb_xbh').html(xbh);
			var xdm = fea.attributes['小地名'];
			$('#xdm').html(xdm);
			$('#xb_xdm').html(xdm);
			var mc = xzm + cm + sm + "社" + xbh
			$('#mc').html(mc);
		},
		getPointXbInfo : function(geo) {
			var map = new mapUtil();
			var identifyTask = new IdentifyTask(constantUtil.LYXB_URL);
			var identifyParams = new IdentifyParameters();
			identifyParams.returnGeometry = false;
			identifyParams.tolerance = 0;
			identifyParams.layerIds = [ 0 ];
			identifyParams.layerOption = IdentifyParameters.LAYER_OPTION_ALL;
			identifyParams.geometry = geo;
			identifyParams.mapExtent = map.__proto__.baselayer.fullExtent;
			var that = this;
			identifyTask.execute(identifyParams, function(response) {
				if (typeof (response[0]) != "undefined") {
					var fea = response[0].feature;
					var fid = fea.attributes['FID'];
					if (fid != $('#hid_input_fid').val()) {
						that.getLyxbLayerInfo(fea);
						$('#hid_input_fid').val(fid);
					}
				} else {
					$('#hid_input_fid').val("999999");
					$('#input_xbh').val("1");
					$('#mc').html("未知小班")
				}
			});
		},
		getNewXbInfoe : function(xbh, cm) {
			var queryTask = new esri.tasks.QueryTask(constantUtil.LYXB_URL + "/0");
			var query = new Query();
			query.outFields = [ "*" ];
			query.where = "小班号 = " + xbh + " and 村= '" + cm + "'";
			query.returnGeometry = false;
			var that = this;
			queryTask.execute(query, function(featureSet) {
				var fea = featureSet.features[0];
				if (typeof (fea) != "undefined") {
					var fid = fea.attributes['FID'];
					if (fid != $('#hid_input_fid').val()) {
						that.getLyxbLayerInfo(fea);
						$('#hid_input_fid').val(fid);
					}
				} else {
					$.alert(cm + "不存在小班号为：" + xbh + "的小班信息");
				}
			});
		},
		listXbmc : function(xzm, xbh) {
			var queryTask = new esri.tasks.QueryTask(constantUtil.LYXB_URL + "/0");
			var query = new Query();
			query.outFields = [ "*" ];
			query.where = "乡 = '" + xzm + "' and 小班号= " + xbh + "";
			query.returnGeometry = false;
			var that = this;
			queryTask.execute(query, function(featureSet) {
				var resultFeatures = featureSet.features;
				$('#select_mc').html("");
				for (var i = 0; i < resultFeatures.length; i++) {
					var fea = featureSet.features[i];
					if (typeof (fea) != "undefined") {
						var xbmc = fea.attributes['名称'];
						var fid = fea.attributes['FID'];
						$('#select_mc').append('<option value="' + fid + '">' + xbmc + '</option>');
					}
				}

			});
		},
		addInfoWindow : function(geo, map, toX, toY) {
			var identifyTask = new IdentifyTask(constantUtil.LYXB_URL);
			var identifyParams = new IdentifyParameters();
			identifyParams.returnGeometry = false;
			identifyParams.tolerance = 4;
			identifyParams.layerIds = [ 0 ];
			identifyParams.mapExtent = map.__proto__.baselayer.fullExtent;
			identifyParams.geometry = geo;
			var that = this;
			identifyTask.execute(identifyParams, function(response) {
				if(response.length == 0){
					map.map.infoWindow.setTitle("Coordinates");
					map.map.infoWindow.setContent(
			            '<div style="background-color:lightblue;color:white;">' + "GPS信号差，精度不准确，无法获取小班信息！"
			        );
			        var point1 = new Point(toX, toY, new SpatialReference({
							wkid : 102113
						}));
			        map.map.infoWindow.show(point1);
				}else if (response.length == 1) {
					var fea = response[0].feature;
					var fid = fea.attributes['FID'];
					var project = new projectUtil();
					if (project.checkCheckAccept(fid) == "true") {
						var qx = fea.attributes['县'];
						var xm = fea.attributes['乡'];
						var cm = fea.attributes['村'];
						var sm = fea.attributes['社'];
						var xbh = fea.attributes['小班号'];
						var xdm = fea.attributes['小地名'];
						var mj = fea.attributes['面积'];
						var population = "fid=" + fid + "&xzm=" + xm + "&cm=" + cm + "&sm=" + sm + "&xbh=" + xbh + "&xdm=" + xdm + "&mj=" + mj;
						map.map.infoWindow.setTitle("Coordinates");
						map.map.infoWindow.setContent(
				            '<div style="background-color:lightblue;color:white;">' + population
				        );
				        var point1 = new Point(toX, toY, new SpatialReference({
								wkid : 102113
							}));
				        map.map.infoWindow.show(point1);
					} else {
						$.alert(project.checkCheckAccept(fid));
					}
				} else {
					var values = new Array();
					for (var i = 0; i < response.length; i++) {
						var fea = response[i].feature;
						var fid = fea.attributes['FID'];
						var xzm = fea.attributes['乡'];
						var cm = fea.attributes['村'];
						var sm = fea.attributes['社'];
						var xbh = fea.attributes['小班号'];
						var mc = xzm + cm + sm + "社" + xbh + "_" + fid;
						values.push(mc);
					}
					map.map.infoWindow.setTitle("Coordinates");
					map.map.infoWindow.setContent(
			            '<div style="background-color:lightblue;color:white;">' + values[0]
			        );
			        var point1 = new Point(toX, toY, new SpatialReference({
							wkid : 102113
						}));
			        map.map.infoWindow.show(point1);
				}
			});
		}
	};
});
